<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>云API</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
    <style>
    body{
      overflow:hidden;
      position:fixed;
      left:0;
      top:1.5rem;
    }
    .header{
          width: 100%;
          height: 2.5rem;
          line-height: 2.5rem;
          padding: 0.5rem 0rem;
          text-align: center;
          position: fixed;
          top:2.5rem;
          background-color: #fff;
      }

    .header ul {
        width: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
    }

    .header ul li {
        width: 30%;
        text-align: center;
        float: left;
        font-family: '宋体'
    }

    .header ul li a {
        color: black;
        text-decoration: none;
        font-weight: bold;
        font-size: 1.2rem;
    }
  .inp{
    width: 80%;
    margin: 3rem auto 0rem;
    margin-top:35%;

  }
  .inp input{
    outline: none;
    width: 100%;
    height: 4rem;
    color: black;
    line-height: 4rem;
    font-size: 1.3rem;
    border-bottom: 1px solid #ccc;
  }
  #name{
    background: url(../image/u14.png) center right;
    background-size: 7% 34%;
    background-repeat: no-repeat;
  }
  #pwd{
    background: url(../image/u15.png) center right;
    background-size: 7% 34%;
    background-repeat: no-repeat;
  }
  .log{
    width: 100%;
    height: 3.5rem;
    background-color: #40cccc;
    color: #fff;
    line-height: 3.5rem;
    text-align: center;
    margin-top: 4rem;
    font-size: 1.2rem;
    border-radius: 2rem;
    letter-spacing:0.1rem;
  }
  .p{
    width: 100%;
    height: 1rem;
    text-align: center;
    line-height: 1rem;
    margin-top: 0.8rem;
  }
  .p1{
    position: absolute;
    top:-0.5rem;
    padding: 0rem 0.8rem;
    left: 37%;
    height: 1rem;
    text-align: center;
    line-height: 1rem;
    background-color: #fff;
  }
  .footer{
    width: 80%;
    position: fixed;
    bottom:1rem;
    left: 10%;
    border-top:1px solid #ccc;
  }
  .footer_img{
    width: 80%;
    margin: 1rem auto;
    display: flex;
    justify-content: space-between;
  }
  .img{
    width: 20%;
    height: 10%;
  }
  .img img{
    width: 100%;
    height: 100%;
  }
    </style>
</head>
<body>
  <div class="header">
      <ul>
          <!-- <li><a href="#">取消</a></li> -->
          <li><a href="#">登录</a></li>
          <li onclick="openwin('zc')"><a href="#" id="a">注册</a>
          </li>

      </ul>
  </div>

<div class="inp">
  <input type="text" name="" placeholder="手机号" id="phone">
  <input type="password" name="" placeholder="密码" id="pwd">
  <!-- <img src="../image/u15.png" style="width:5rem;height:5rem" onclick="settype()"> -->
  <div class="log" onclick="login_at()">登录</div>
  <p class="p" onclick="openwin('get_pass')">忘记了密码?</p>
</div>
<!-- <div class="footer" id="footer">
  <p class="p1">快捷登录</p>
  <div class="footer_img">
    <div class="img">
      <img src="../image/u8.png" alt="">
    </div>
    <div class="img">
      <img src="../image/u9.png" alt="">
    </div>
    <div class="img">
      <img src="../image/u10.png" alt="">
    </div>
  </div>
</div> -->
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.3.js"></script>
<script type="text/javascript">
var oHeight = $(document).height(); //屏幕当前的高度

        $(window).resize(function(){

                if($(document).height() < oHeight){

                $(".footer").css("display","none");

            }else{

                $(".footer").css("display","block");

            }

            });


var userinfo = $api.getStorage('userinfo');
var type = 0
apiready = function() {
  api.addEventListener({
      name:'user_register',
      extra:{
          threshold:0
      }
  }, function(ret, err){
      alert('注册成功')
      api.closeWin({

      });


        //alert( JSON.stringify( ret ) );
  });




      api.addEventListener({
        name: 'keyback'
      }, function(ret, err){
        api.confirm({
            title: '是否确认退出课包?',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            if (ret.buttonIndex == 1) {
              api.closeWidget({
                  id : 'A6036854274281',  //你的APPid
                  retData : {
                      name : 'closeWidget'
                  },
                  animation : {
                      type : 'flip',
                      subType : 'from_bottom',
                      duration : 500
                  },
                  silent : true
              });
            }
        });
      });


      api.addEventListener({
          name:'addjl_ok',
          extra:{
              threshold:0
          }
      }, function(ret, err){
        api.closeWin({

        });

      })




}




function login_at() {
    var phone = document.getElementById('phone').value;
    if(!(/^1[3456789]\d{9}$/.test(phone))){
        msgcode("手机号码有误，请重填");
        return false;
    }
    var password = document.getElementById('pwd').value;

    if(password==''){
      msgcode("请输入密码");
      return false;
    }


    api.ajax({
        url: SITE_URL+'user_api/login/index',
        method: 'post',
        data: {
            values: {
                phone: phone,
                pwd:password
            }
        }
    },function(ret, err){
        if (ret) {
          if(ret.error==200){
            msgcode('登陆成功')
            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '正在设置...',
                text: '先喝杯茶...',
                modal: false
            });


            set_ry_token(ret.data)



          }else{
            msgcode('账号密码错误')
          }
            //alert( JSON.stringify( ret ) );
        } else {
          msgcode('请检查网络连接')
            //alert( JSON.stringify( err ) );
        }
    });

}

function set_ry_token(user_info) {
  var userId = user_info.id
                                //会员id
                                var name = user_info.nick_name;
                                //会员昵称
                                var portraitUri = user_info.head_img;
                                //会员头像

   //                             var appKey = "pwe86ga5p99q6";
   //                             var appSecret = "tTNc5tntn1oc";

    var appKey = "kj7swf8oknpa2";
    var appSecret = "Zm3i7peeLZBsi4";
                                var nonce = Math.floor(Math.random() * 1000000);
                                //随机数
                                var timestamp = Date.now();
                                //时间戳
                                var signature = sha1("" + appSecret + nonce + timestamp);
//start
    api.hideProgress();
    $api.setStorage('userinfo',user_info);
    api.sendEvent({
        name: 'user_login',
    });
    api.closeWin({

    });
    $api.setStorage('ry_token', user_info.token);
    //end
                                //数据签名(通过哈希加密计算)
                                api.ajax({
                                        url : "http://api.cn.ronghub.com/user/getToken.json",
                                        method : "post",
                                        headers : {
                                                "RC-App-Key" : appKey,
                                                "RC-Nonce" : "" + nonce,
                                                "RC-Timestamp" : "" + timestamp,
                                                "RC-Signature" : "" + signature,
                                                "Content-Type" : "application/x-www-form-urlencoded"
                                        },
                                        data : {
                                                values : {
                                                        userId : userId,
                                                        name : name,
                                                        portraitUri : portraitUri
                                                }
                                        }
                                }, function(ret, err) {

                                        if (ret) {
                                          api.hideProgress();
                                          $api.setStorage('userinfo',user_info);
                                          api.sendEvent({
                                              name: 'user_login',
                                          });
                                          api.closeWin({

                                          });
                                                $api.setStorage('ry_token', ret.token);
                                                //将token存储到本地
                                            //    alert("你的token是："+ret.token);
                                        } else {
                                          api.hideProgress();
                                          msgcode('设置个人信息失败,请稍后登陆')
                                                //alert("获取token失败")
                                        }
                                });
}

function settype() {
   var input = document.getElementById("pwd");
     if(input.type=="password"){
       input.type='text';
     }else{
       input.type='password';
     }

}
</script>
</html>
